package com.wanmait.recycle.webcontroller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.EmailSend;

@Controller
@RequestMapping("web/userLogin")
public class UserChangePassController {

	@Resource
	private UserinfoService userinfoService;
	
	@RequestMapping("changeFir")
	public void changeFir() {
		
	}
	
	
	@RequestMapping(value="changeSec",method=RequestMethod.GET)
	public void changeSec() {
		
	}
	
	
	@RequestMapping(value="changeSec",method=RequestMethod.POST)
	public ModelAndView changeSec1(HttpSession session,String checkCode) {
		
		ModelAndView mv = new ModelAndView();
		String code = (String)session.getAttribute("code");
		Userinfo changeUser = (Userinfo)session.getAttribute("changePassUser");
		if(!code.equalsIgnoreCase(checkCode)) {
			mv.setViewName("redirect:/web/userLogin/changeFir");
		}else {			
			String mailCode = "";
			try {
				mailCode = EmailSend.getEmailSend().sendEmail(changeUser.getMailBox());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			session.setAttribute("mailCode", mailCode);
			
			String mailBox = changeUser.getMailBox();
			String[] str = mailBox.split("@");
			String str1 = str[0].substring(0, 5);
			String newMail = str1 + "*******@"+str[1];
			
			mv.addObject("mailbox", newMail);
		}
		return mv;
	}
	
	@RequestMapping(value="changeThird",method=RequestMethod.GET)
	public void changeThird1() {
		
	}
	
	@RequestMapping(value="changeThird",method=RequestMethod.POST)
	public ModelAndView changeThird(String mailCode,HttpSession session) {
		
		ModelAndView mv = new ModelAndView();
		String code = (String)session.getAttribute("mailCode");
		if(!code.equalsIgnoreCase(mailCode)) {
			mv.setViewName("redirect:/web/userLogin/changeSec");
			return mv;
		}
		
		return mv;
	}
	
	@RequestMapping(value="changeForth",method=RequestMethod.GET)
	public void changeForth() {
		
	}
	
	@RequestMapping(value="changeForth",method=RequestMethod.POST)
	public void changeForth1(String newPass,HttpSession session) {
		Userinfo changeUser = (Userinfo)session.getAttribute("changePassUser");
		
		userinfoService.updatePasswordByName(changeUser.getName(), newPass);
		
	}
	
	
	
	
	
	
	@RequestMapping("sendEmail")
	@ResponseBody
	public boolean sendEmail(String mailbox,HttpSession session) {
	
		String mailCode = "";
		try {
			mailCode = EmailSend.getEmailSend().sendEmail(mailbox);
		} catch (Exception e) {
			
			return false;
		}	
		session.setAttribute("mailCode", mailCode);
		return true;
	}
	
	
	
	@RequestMapping("getCode")
	@ResponseBody
	public String getCode(HttpSession session) {
		
		return (String)session.getAttribute("code");
	}
	
	//页面获得验证码
	@RequestMapping("mailCode")
	@ResponseBody
	public String getmailCode(HttpSession session) {
		
		return (String)session.getAttribute("mailCode");	
	}
	
	//判断是否有该用户
	@RequestMapping("isHaveUser")
	@ResponseBody
	public boolean isHaveUser(String str,String mother,HttpSession session) {
		
		boolean flag =false;
		
		Userinfo userinfo = userinfoService.isHaveUser(str,mother);
		
		session.setAttribute("changePassUser", userinfo);
		
		if(userinfo==null) {
			
			flag=false;
		}else {
			flag=true;
		}
		
		
		return flag;
	}
	
	
	
}
